package com.xinruke.hostels.manage.user.interfaces.api;

import com.xinruke.hostels.common.fallback.FeignFallback;
import com.xinruke.hostels.common.vo.ResultVO;
import com.xinruke.hostels.manage.user.interfaces.dto.UserDetailQueryDTO;
import com.xinruke.hostels.manage.user.interfaces.dto.UserDetailQueryResultDTO;
import com.xinruke.hostels.manage.user.interfaces.dto.UserInfoAddDefaultDTO;
import com.xinruke.hostels.manage.user.interfaces.dto.login.LoginManageValidateDTO;
import com.xinruke.hostels.manage.user.interfaces.dto.login.LoginManageValidateResultDTO;
import feign.hystrix.FallbackFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.validation.Valid;

/**
 * @ClassName LoginFallback
 * @CreateDate 2019/6/12
 * @Author FengXinQiang
 * @Version V1.0.0.0
 * @Decription 登录API熔断
 */
@Component
public class ManageUserFallback implements FallbackFactory<ManageUserApi> {
    Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private FeignFallback feignFallback;

    @Override
    public ManageUserApi create(Throwable throwable) {
        logger.error("系统异常", throwable);

        return new ManageUserApi() {
            @Override
            public ResultVO<LoginManageValidateResultDTO> loginValidate(LoginManageValidateDTO loginValidateDTO) {
                return feignFallback.doFallback(ManageUserFallback.class, "loginValidate", "登录验证");
            }

            @Override
            public ResultVO<UserDetailQueryResultDTO> detail(@Valid UserDetailQueryDTO userDetailQueryDTO) {
                return feignFallback.doFallback(ManageUserFallback.class, "detail", "用户详情");
            }

            @Override
            public ResultVO addHotelUserInfo(@Valid UserInfoAddDefaultDTO userInfoAddDTO) {
                return feignFallback.doFallback(ManageUserFallback.class, "addHotelUserInfo", "添加用户");
            }
        };
    }
}
